package com.inews.business.web;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.inews.base.exception.BusinessException;
import com.inews.base.mapper.JsonMapper;
import com.inews.base.utils.InewsUtils;
import com.inews.base.utils.Servlets;
import com.inews.base.vo.ExtData;
import com.inews.business.service.RetireStaffService;
import com.inews.business.vo.RetireStaffVo;

@Controller
@RequestMapping("/retireStaff")
public class RetireStaffController {
	
	@Autowired
	private RetireStaffService service;
	
	
	/**
	 * 分页查询退休人员信息.
	 *
	 * @param request the request
	 * @param pageNumber the page number
	 * @param sort the sort
	 * @return the object
	 */
	@RequestMapping("findRetireStaffPage")
	@ResponseBody
	public Object findRetireStaffPage(ServletRequest request, @RequestParam(value = "page", defaultValue = "1") int pageNumber, String sort){
		Pageable page = Servlets.buildPageRequest(pageNumber, sort);
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		ExtData<RetireStaffVo> extData = service.findRetireStaffPage(searchParams, page);
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	/**
	 * 保存退休人员信息.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("saveRetireStaff")
	@ResponseBody
	public Object saveRetireStaff(RetireStaffVo vo){
		ExtData<RetireStaffVo> extData = service.saveRetireStaff(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	/**
	 * 更新退休人员信息.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("updateRetireStaff")
	@ResponseBody
	public Object updateRetireStaff(RetireStaffVo vo){
		ExtData<RetireStaffVo> extData = service.updateRetireStaff(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	/**
	 * 删除退休人员信息.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("deleteRetireStaff")
	@ResponseBody
	public Object deleteRetireStaff(RetireStaffVo vo){
		ExtData<RetireStaffVo> extData = service.deleteRetireStaff(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	
	/**
	 * 报表导出.
	 *
	 * @return the model and view
	 */
	@RequestMapping("exportRetireStaffExcel")
	public String exportRetireStaffExcel(){
		return "retireStaffExcelView";
	}
	
	
	/**
	 * 报表导入.
	 *
	 * @return the model and view
	 */
	@RequestMapping("importExcel")
	@ResponseBody
	public String importExcel(@RequestParam("excelFile") MultipartFile excelFile){
		InputStream is;
		try {
			is = excelFile.getInputStream();
			ExtData<String> extData = service.importExcel(is, InewsUtils.getUserInfo());
			String json = JsonMapper.nonEmptyMapper().toJson(extData);
			return json;
		} catch (IOException e) {
			throw new BusinessException("导入失败");
		}
	}

}
